import csv, time, pprint, math, re
from numpy import numarray
    
class Transmitter:
  def __init__(self):
    self.main_id = None
    self.sub_id = None
    self.tx_area = None
    self.site = None
    self.latlon = (0, 0)
    self.grid_reference = None
    self.site_height = None
    self.in_use_ae_height = None
    self.in_use_erp_total = None
    self.dir_max_erp = None
    self.something_erp = None
    
class DABEnsemble:
  def __init__(self, eid=None, name=None, frequency=None, block=None, licence=None):
    self.eid = eid.lower()
    self.name = name
    self.frequency = frequency
    self.block = block
    self.licence = None
    self.services = {}
    self.data_services = {}
    
  def __repr__(self):
    return "%s - %s" % (self.eid, self.name)
    
  def addService(self, service):
    if service.sid not in self.services:
      self.services[service.sid] = service
      return True
    else:
      return False
      
  def addDataService(self, service):
    if service.sid not in self.services:
      self.services[service.sid] = service
      return True
    else:
      return False
    
class DABService:
  def __init__(self, name=None, sid=None, other_id=None):
    self.sid = sid.lower()
    self.name = name
    self.other_id = other_id
    
  def __repr__(self):
    return "%s - %s (%s)" % (self.sid, self.name, self.url)
    
class MFService:
  def __init__(self, row):
    self.date = row[0]
    self.name = row[1]
    self.area = row[2]
    self.site = row[3]
    self.licence = row[4]
    self.frequency = row[5]
    self.grid_reference = row[6]
    self.site_height = row[7]
    self.in_use_ae_height = row[8]
    self.in_use_emrp = row[9]

class VHFService:
  def __init__(self, row):
    self.date = row[0]
    self.name = row[1]
    self.area = row[2]
    self.site = row[3]
    self.licence = row[4]
    self.frequency = row[5]
    self.grid_reference = row[6]
    self.rds_ps = row[7]
    self.rds_pi = row[8]
    self.switched_pi = row[9]
    self.tp_ta = row[10]
    self.tmc = row[11]
    self.site_height = row[12]
    self.in_use_ae_height = row[13]
    self.in_use_erp_hp = row[14]
    self.in_use_erp_vp = row[15]
    self.max_dir = row[16]
    
class DABDataService:
  def __init__(self):
    pass
    
class TxParse:
  def __init__(self):
    self.dabservice = {}
    self.dabensemble = {}
    self.mfservice = {}
    self.vhfservice = {}
    self.transmitter = {}
    self.dabensemble_has_service = {}
    self.dabensemble_has_transmitter = {}

  def parseDabSheet(self, filename):
    # Open file and read lines
    fileinput = open(filename)
    lines = fileinput.readlines()
    # Parse each line as CSV, ignoring first two (headers)
    data = iter(lines[2:])
    reader = csv.reader(data)
    # Parse each individual row, one at a time
    for row in reader:
      # Maintain a count 
      count = 0
      # Parse ensemble
      if row[4] in self.dabensemble:
        pass
      else:
        e = DABEnsemble(row[4], row[1], row[7], row[8], row[2])
        rawservices = numarray.reshape(row[11:56], 15, 3)
        for s in rawservices:
          if s[0] != "":
            service = DABService(s[0], s[1], s[2])
          e.addService(service)
        self.dabensemble[row[4]] = e

  def parseVhfSheet(self, filename):
    # Open file and read lines
    fileinput = open(filename)
    lines = fileinput.readlines()
    # Parse each line as CSV, ignoring first two (headers)
    data = iter(lines[2:])
    reader = csv.reader(data)
    # Parse each individual row, one at a time
    for row in reader:
      self.vhfservice[row[1]] = VHFService(row)
    pass

  def parseMfSheet(self, filename):
    # Open file and read lines
    fileinput = open(filename)
    lines = fileinput.readlines()
    # Parse each line as CSV, ignoring first two (headers)
    data = iter(lines[2:])
    reader = csv.reader(data)
    # Parse each individual row, one at a time
    for row in reader:
      self.mfservice[row[1]] = MFService(row)

txp = TxParse()

txp.parseDabSheet('data/dab.csv')
txp.parseVhfSheet('data/vhf.csv')
txp.parseMfSheet('data/mf.csv')

for k, ensemble in txp.dabensemble.items():
  for k, v in ensemble.services.items():
    print v.name
    
for k, v in txp.vhfservice.items():
  print v.name, v.frequency
    
for k, v in txp.mfservice.items():
  print v.name, v.frequency